﻿using System;

class LongestSequenceOfStrings

            // We are given a matrix of strings of size N x M. Sequences in the matrix we define as sets of several neighbor elements located on the same line, 
            // column or diagonal. Write a program that finds the longest sequence of equal strings in the matrix.
{
    static void Main()
    {
        Console.Write("Enter number of rows 'N': ");
        int rowsN = int.Parse(Console.ReadLine());
        Console.Write("Enter number of rows 'M': ");
        int colsM = int.Parse(Console.ReadLine());
        string[,] matrix = new string[rowsN, colsM];
        Console.WriteLine();

        for (int row = 0; row < rowsN; row++)
        {
            for (int col = 0; col < colsM; col++)
            {
                Console.Write("String on position [{0},{1}] = ", row, col);
                matrix[row, col] = Console.ReadLine();
            }
        }
        Console.WriteLine();

        int maxCount = 0;
        string maxSeq = "";

        for (int i = 0; i < rowsN; i++)
        {
            for (int j = 0; j < colsM; j++)
            {
                int horCount = 0;
                int verCount = 0;

                while (i + horCount < rowsN)
                {
                    if (matrix[i, j] == matrix[i + horCount, j])
                    {
                        horCount++;
                    }
                    else
                    {
                        break;
                    }
                }
                if (horCount + 1 > maxCount)
                {
                    maxCount = horCount;
                    maxSeq = matrix[i, j];
                }
                while (j + verCount < colsM)
                {
                    if (matrix[i, j] == matrix[i, j + verCount])
                    {
                        verCount++;
                    }
                    else
                    {
                        break;
                    }
                }
                if (verCount + 1 > maxCount)
                {
                    maxCount = verCount;
                    maxSeq = matrix[i, j];
                }
                while (i + horCount < rowsN && j + horCount < colsM)
                {
                    if (matrix[i, j] == matrix[i + horCount, j + horCount])
                    {
                        horCount++;
                    }
                    else
                    {
                        break;
                    }
                }
                if (horCount + 1 > maxCount)
                {
                    maxCount = horCount;
                    maxSeq = matrix[i, j];
                }
            }
        }
        Console.WriteLine("The longest sequence of equal strings is: \n");
        for (int i = 0; i < maxCount; i++)
        {
            Console.Write("{0,3}", maxSeq);
        }
        Console.WriteLine();
    }
}

